//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.Common.Utils;
using System.Web.Security;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CustomerLoginControl : BaseNopFrontendUserControl
    {
        private void ApplyLocalization() {
            if (this.CustomerService.UsernamesEnabled) {
                lblUsernameOrEmail.Text = GetLocaleResourceString("Login.Username");
            }
            else {
                lblUsernameOrEmail.Text = GetLocaleResourceString("Login.E-MailAddress");
            }
            if (this.CustomerService.UsernamesEnabled) {
                UserNameOrEmailRequired.ErrorMessage = GetLocaleResourceString("Login.UserNameRequired");
                UserNameOrEmailRequired.ToolTip = GetLocaleResourceString("Login.UserNameRequired");
            }
            else {
                UserNameOrEmailRequired.ErrorMessage = GetLocaleResourceString("Login.E-MailRequired");
                UserNameOrEmailRequired.ToolTip = GetLocaleResourceString("Login.E-MailRequired");
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
            this.ApplyLocalization();

            pnlError.Visible = false;

            if (this.SettingManager.GetSettingValueBoolean("Common.LoginCaptchaImageEnabled")) {
                ctrlCaptcha.Visible = true;
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e) {
            string url = CommonHelper.GetStoreLocation() + "register.aspx";
            string returnUrl = CommonHelper.QueryString("ReturnUrl");
            if (!String.IsNullOrEmpty(returnUrl)) {
                url = CommonHelper.ModifyQueryString(url, "ReturnUrl=" + returnUrl, null);
            }
            Response.Redirect(url);
        }

        protected void btnLogin_Click(object sender, EventArgs e) {
            Page.Validate("LoginValidationGroup");
            if (!Page.IsValid) {
                pnlError.Visible = true;
                litError.Text = GetLocaleResourceString("Login.FailureText");
            }
            else {
                if (this.SettingManager.GetSettingValueBoolean("Common.LoginCaptchaImageEnabled") && !ctrlCaptcha.ValidateCaptcha()) {
                    ctrlCaptcha.RegenerateCode();
                    pnlError.Visible = true;
                    litError.Text = GetLocaleResourceString("Login.FailureText");
                    return;
                }

                if (Membership.ValidateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim())) {
                    FormsAuthentication.SetAuthCookie(txtUserName.Text.Trim(), chkRememberMe.Checked);

                    string redirectTo = CommonHelper.GetStoreLocation();
                    if (!string.IsNullOrWhiteSpace(Request.QueryString["ReturnUrl"])) {
                        redirectTo = Request.QueryString["ReturnUrl"];
                    }
                    Response.Redirect(redirectTo);
                }
                else {
                    pnlError.Visible = true;
                    litError.Text = GetLocaleResourceString("Login.FailureText");
                }
            }
        }

        protected void btnCheckoutAsGuest_Click(object sender, EventArgs e) {
            string url = CommonHelper.GetStoreLocation() + "checkout.aspx";
            Response.Redirect(url);
        }

        public bool CheckoutAsGuestQuestion {
            get {
                bool checkoutAsGuest = CommonHelper.QueryStringBool("CheckoutAsGuest");
                return checkoutAsGuest && this.CustomerService.AnonymousCheckoutAllowed;
            }
        }
    }
}